<?php
/**
 *
 * description...
 *
 * @author wanghaifei <wanghaifei@soften.cn>
 *
 */


/**
 * 通过CURL方式请求链接
 * @param 链接地址 $url
 * @param 传输数据 $post_string
 * @param 传输数据 $header
 * @param 传输数据 $cookie
 * @param 连接超时时间 $connectTimeout
 * @param 读取数据超时时间 $readTimeout
 */

function byCurlRequest($url, $post_string='', $header='', $cookie='', $referer='', $connectTimeout = 10, $readTimeout = 20)
{
        $result = "";
        if (function_exists('curl_init')) {
			$timeout = $connectTimeout + $readTimeout;
			// Use CURL if installed...
			$ch = curl_init();
			curl_setopt($ch, CURLOPT_URL, $url);
			curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $connectTimeout);
			curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
			if(!empty($post_string))
			{
					curl_setopt($ch, CURLOPT_POST, true);
					curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
			}
			
			if(!empty($header))
			{
				curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
			}
			if(!empty($cookie))
			{   
				curl_setopt($ch,CURLOPT_COOKIEFILE,$cookie);
			}
			if(!empty($referer))
			{   
				curl_setopt($ch, CURLOPT_REFERER, $referer);   //构造来路
			}
			
			//curl_setopt($ch, CURLOPT_HEADER, 1);
			
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

			$result = curl_exec($ch);

			curl_close($ch);
				
        }
        return $result;
}

 function curlProxy($proxy_ip, $request_url, $post_string='', $cookie='', $header='', $proxy_port=20068){
    
	$result = "";

	if (function_exists('curl_init')) {
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $request_url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_PROXY, $proxy_ip);
		curl_setopt($ch, CURLOPT_PROXYPORT, $proxy_port);
		curl_setopt($ch, CURLOPT_TIMEOUT, 30);

		if(!empty($post_string))
		{
			curl_setopt($ch, CURLOPT_POST, true);
			curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
		}
		if(!empty($cookie))
		{   
			curl_setopt($ch,CURLOPT_COOKIEFILE,$cookie);
		}
		if(!empty($header))
		{
			curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
		}
		$result = curl_exec($ch);

		curl_close($ch);	
	}
	return $result;
}


/**
 * 获取cookie文件地址
 *
 * @param string $sourceName
 * @return string
 */
function  getCookieFile($sourceName)
{
   $cookie_path = ROOTPATH.COOKIE_PATH;
	
	$fileName = '';

    if($sourceName=='tencent')
	{
	    $fileName = $cookie_path.'cookie_tx_NTEzOTE3Mj';
	}
	elseif($sourceName=='sohu')
	{
	    $fileName = $cookie_path.'cookie_sh_NTEzOTE3Mj';
	}

	return $fileName;

}

/**
 * 对json_encode中文字符串进行反转
 *
 * @param array or string $data
 * @return array or string
 */

function iconv_decode($data){
    
	if(empty($data)) return false;

    if(is_array($data))
	{   
		$dataArr = array();
	    foreach($data as $key => $value)
		{
		    $dataArr[] = iconv_decode($value);
		}
		return $dataArr;
	}

    $preg_array = array("/\\\uffff/", "/\\\u([0-9a-f]{4})/ie");
    $replace_array = array("", "iconv('UCS-2BE', 'UTF-8//TRANSLIT//IGNORE', pack('H4', '\\1'))");
    $data = preg_replace($preg_array, $replace_array, $data);

           
	return $data;

}


/**
 * $str 原始中文字符串
 * $encoding 原始字符串的编码，默认GBK
 * $prefix 编码后的前缀，默认"&#"
 * $postfix 编码后的后缀，默认";"
 */
function unicode_encode($str, $encoding = 'UTF-8', $prefix = '&#', $postfix = ';') {
    $str = iconv($encoding, 'UCS-2', $str);
    $arrstr = str_split($str, 2);
    $unistr = '';
    for($i = 0, $len = count($arrstr); $i < $len; $i++) {
        $dec = hexdec(bin2hex($arrstr[$i]));
        $unistr .= $prefix . $dec . $postfix;
    }
    return $unistr;
}
 
/**
 * $str Unicode编码后的字符串
 * $decoding 原始字符串的编码，默认GBK
 * $prefix 编码字符串的前缀，默认"&#"
 * $postfix 编码字符串的后缀，默认";"
 */
function unicode_decode($unistr, $encoding = 'UTF-8', $prefix = '&#', $postfix = ';', $codelen = 5) {
    $arruni = explode($prefix, $unistr);
    $unistr = '';
    for($i = 0, $len = count($arruni); $i < $len; $i++) {
        if (strlen($postfix) > 0) {
            if(! preg_match_all('|.*?\d{'.$codelen.'}.*?|ims',$arruni[$i], $out)){
                $unistr .= $arruni[$i];
                continue;
            }
            if (strlen($arruni[$i]) > ($codelen + $postfix)) {
               list($code, $str) = explode($postfix, $arruni[$i],2);
               if(ctype_digit($code)  && (strlen($code) == $codelen)){
                   $ascii = ($code < 256) ? chr(0) . chr($code) : chr($code / 256) . chr($code % 256);
                   $unistr .= iconv('UCS-2BE', $encoding, $ascii);
               }
               $unistr .= $str;
            }else if(strlen($arruni[$i]) == ($codelen + $postfix)){
                $code = intval($arruni[$i]);
                if(ctype_digit($code) && (strlen($code) == $codelen)){
                    $ascii = ($code < 256) ? chr(0) . chr($code) : chr($code / 256) . chr($code % 256);
                    $unistr .= iconv('UCS-2BE', $encoding, $ascii);
                }else{
                    $unistr .= $arruni[$i];
                }
            }
        }
    }
    return $unistr;
}

/*
* 对象转换为数组 
* return array
*/
function object_to_array($obj){

$_arr = is_object($obj) ? get_object_vars($obj) : $obj;
foreach ($_arr as $key => $val)
{
$val = (is_array($val) || is_object($val)) ? object_to_array($val) : $val;
$arr[$key] = $val;
}
return $arr;
}

/*
* 删除数组元素
*/
function array_unset($arr_data,$unset){

    if(!is_array($unset)) return false;
    
	foreach($unset as $val)
	{
	    unset($arr_data[$val]);
	}

	return $arr_data;
}
?>